<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <el-table :data="rolelist" stripe style="width: 100%" :border="true">
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- el-row 代表每一行 -->
            <el-row
              class="el-row-biaoge"
              v-for="item1 in scope.row.children"
              :key="item1.id"
            >
              <!-- 第一列 -->
              <el-col :span="5"
                ><el-tag
                  closable
                  @close="deletRightsById(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 第二列 -->
              <el-col :span="19" class="el-row-er">
                <el-row v-for="item2 in item1.children" :key="item2.id">
                  <el-col :span="6"
                    ><el-tag
                      closable
                      type="success"
                      @close="deletRightsById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 第三列 -->
                  <!--删除时需传入 scope.row,item.id 两个id -->
                  <el-col :span="18">
                    <el-tag
                      closable
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      @close="deletRightsById(scope.row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <pre>{{ scope.row }}</pre>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column prop="address" label="操作" width="300px">
          <!-- 只能通过模板引擎 scope.row 取到id  -->
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              size="mini"
              @click="fenPeiQuanXianClick(scope.row)"
              type="warning"
              icon="el-icon-setting"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      @close="closeDialog"
      :visible.sync="rightsDialogVisible"
      width="50%"
    >
      <el-tree
        :data="rightslist"
        default-expand-all
        node-key="id"
        :default-checked-keys="defKeys"
        show-checkbox
        :props="defaultProps"
        ref="rightsTree"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolelist: [],
      rightsDialogVisible: false,
      rightslist: [],
      defaultProps: {
        //二级和三级的数据来源
        children: "children",
        // 渲染到页面的数据
        label: "authName",
      },
      //默认选中节点的数组
      defKeys: [],
      roleId: "",
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    // 渲染
    async getRolesList() {
      const { data: res } = await this.$axios.get("roles");
      // console.log(res);
      if (res.meta.status != 200) {
        return this.$message.error("获取失败");
      }
      this.$message.success(res.meta.msg);
      this.rolelist = res.data;
    },
    // 删除
    async deletRightsById(row, rightId) {
      //- 请求路径：roles/:roleId/rights/:rightId
      // - 请求方法：delete
      // - 请求参数
      //参数名     	参数说明 	备注
      //:roleId 	角色 ID	不能为空携带在url中
      //:rightId
      var roleId = row.id;
      const { data: res } = await this.$axios.delete(
        `roles/${roleId}/rights/${rightId}`
      );
      // console.log(res);
      if (res.meta.status != 200) {
        return this.$message.error("删除失败");
      }
      // console.log(res.data);
      row.children = res.data;
    },
    // 分配权限--获取所有的权限列表
    async fenPeiQuanXianClick(row) {
      const { data: res } = await this.$axios.get("rights/tree");
      // console.log(res);
      if (res.meta.status != 200) {
        return this.$message.error("获取失败");
      }
      this.roleId = row.id;
      this.$message.success(res.meta.msg);
      this.rightslist = res.data;
      // console.log(this.rightslist);
      // 分配权限--拿到选中的数据 递归recursive
      // console.log(row.children);
      // this.defKeys 放数据的
      this.recursive(row, this.defKeys);
      // console.log(this.defKeys);
      this.rightsDialogVisible = true;
    },
    //递归函数
    recursive(res, arr) {
      if (!res.children) {
        arr.push(res.id);
        return;
      }
      res.children.forEach((element) => {
        this.recursive(element, arr);
      });
    },
    async setRights() {
      //- 请求路径：roles/:roleId/rights
      // - 请求方法：post
      //rids 列表中所有选中的节点 -->全选 getCheckedKeys  半选getHalfCheckedKeys
      // console.log(this.$refs.rightsTree.getCheckedKeys());
      // console.log(this.$refs.rightsTree.getHalfCheckedKeys());
      const rid = [
        ...this.$refs.rightsTree.getCheckedKeys(),
        ...this.$refs.rightsTree.getHalfCheckedKeys(),
      ];
      const ridsid = rid.join(",");
      // console.log(ridsid);  //拿到了rids
      // console.log(this.roleId);
      const { data: res } = await this.$axios.post(
        `roles/${this.roleId}/rights`,
        {
          rids: ridsid,
        }
      );
      // console.log(res.meta.msg);
      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg);
      }
      this.$message.success(res.meta.msg);
    },
    closeDialog() {
      this.defKeys = [];
    },
  },
};
</script>

<style scoped>
.el-row-biaoge,
.el-row-er {
  border-bottom: 1px solid #eee;
}
.el-row-biaoge:first-child {
  border-bottom: 1px solid #eee;
}
.el-row-er {
  border: none;
}
.el-row {
  display: flex;
  align-items: center;
}
.el-tag {
  margin: 7px;
}
</style>